import { defineStore } from 'pinia';
import { maLogin, userLogin, phoneLogin, passwordLogin, quickLogin, userLogout, getUserInfo } from '@/api/login';
import { getCount } from '@/api/order/shoppingCart';
import { Local } from '@/utils/storage';
interface userInfoState {
	userInfo: {
		userNumber: number | undefined;
		nickName: string;
		phone: string;
		userGrade: string;
		avatarUrl: string;
		couponCount: number;
		province: string;
		country: string;
		city: string;
		sex: string;
	};
	shoppingCartCount: number;
	isLogin: boolean;
	token: string;
}
export const useUserStore = defineStore('user', {
	state: (): userInfoState => ({
		userInfo: {
			userNumber: undefined,
			nickName: '',
			phone: '',
			userGrade: '',
			avatarUrl: '',
			couponCount: 0,
			province: '',
			country: '',
			city: '',
			sex: '',
		},
		shoppingCartCount: 0,
		isLogin: false,
		token: Local.getToken(),
	}),
	actions: {
		async maLogin() {
			return new Promise((resolve, reject) => {
				// 小程序登录
				// #ifdef MP-WEIXIN
				uni.login({
					success: function (res) {
						if (res.code) {
							maLogin({
								jsCode: res.code,
							})
								.then((response) => {
									if (response.data.tokenValue) {
										// 存储token 信息
										this.token = response.data.tokenValue;
										Local.setToken(response.data.tokenValue);
									} else {
										// 存储id
										Local.set('wxUserId', response.data);
									}
									resolve(response);
								})
								.catch((err) => {
									reject(err);
								});
						}
					},
				});
				// #endif
			});
		},
		/**
		 * 手机验证码登录方法
		 */
		async phoneLogin(data: any) {
			return new Promise((resolve, reject) => {
				phoneLogin(data)
					.then((response) => {
						// 存储token 信息
						Local.setToken(response.data.tokenValue);
						this.token = response.data.tokenValue;
						this.isLogin = true;
						resolve(response);
					})
					.catch((err) => {
						reject(err);
					});
			});
		},
		/**
		 * 手机号密码登录方法
		 */
		async passwordLogin(data: any) {
			return new Promise((resolve, reject) => {
				passwordLogin(data)
					.then((response) => {
						// 存储token 信息
						Local.setToken(response.data.tokenValue);
						this.token = response.data.tokenValue;
						this.isLogin = true;
						resolve(response);
					})
					.catch((err) => {
						reject(err);
					});
			});
		},
		/**
		 * 小程序手机号快速登录
		 */
		async quickLogin(data: any) {
			return new Promise((resolve, reject) => {
				quickLogin(data)
					.then((response) => {
						// 存储token 信息
						Local.setToken(response.data.tokenValue);
						this.token = response.data.tokenValue;
						this.isLogin = true;
						resolve(response);
					})
					.catch((err) => {
						reject(err);
					});
			});
		},

		/**
		 * 获取用户信息
		 */
		async getInfo() {
			return new Promise((resolve, reject) => {
				getUserInfo({})
					.then((response) => {
						// 存储用户信息
						this.userInfo = response.data;
						Local.set('userInfo', response.data);
						this.isLogin = true;
						resolve(response);
					})
					.catch((err) => {
						reject(err);
					});
			});
		},
		async logout() {
			return new Promise((resolve, reject) => {
				userLogout({})
					.then((response) => {
						// 存储用户信息
						Local.clear();
						this.isLogin = false;
						resolve(response);
					})
					.catch((err) => {
						reject(err);
					});
			});
		},
		/**
		 * 获取已登录用户购物车数量
		 */
		async getCartCount() {
			return new Promise((resolve, reject) => {
				getCount({})
					.then((response) => {
						// 存储信息
						this.shoppingCartCount = response.data;
						uni.setTabBarBadge({
							index: 2,
							text: this.shoppingCartCount + '',
						});
						resolve(response);
					})
					.catch((err) => {
						reject(err);
					});
			});
		},
	},
});
